package com.decent.controller;

import com.decent.entity.Brand;
import com.decent.entity.common.Message;
import com.decent.entity.common.PageResult;
import com.decent.service.BrandService;
import com.decent.enums.MessageEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * @Author decent
 * @Create 2021/4/7 - 16:26
 * @Description
 */
@RestController
public class BrandController {

    @Resource
    private BrandService brandService;

    /**
     * 获取热门品牌
     * @param pageNumber 页码
     * @param pageSize 每页数量
     * @return Message
     */
    @RequestMapping("/brand/hot")
    public Message listBrandHot(@RequestParam(defaultValue = "1") @Min(1) Integer pageNumber,
                                @RequestParam(defaultValue = "3") @Min(1) Integer pageSize) {
        PageInfo<Brand> pageInfo = brandService.listHotBrand(pageNumber, pageSize);
        PageResult pageResult = new PageResult(pageInfo);

        return new Message(MessageEnum.GENERAL_REQUEST_SUCCESS, pageResult);
    }

    /**
     * 获取品牌列表
     * @param brandTypeId 产品类型ID
     * @param pageNumber 页码
     * @param pageSize 每页数量
     * @return
     */
    @RequestMapping("/brand/list")
    public Message listBrand(@NotNull(message = "品牌类型id为空") @Min(1) Long brandTypeId,
                             @RequestParam(defaultValue = "1") @Min(1)  Integer pageNumber,
                             @RequestParam(defaultValue = "3") @Min(1) Integer pageSize) {
        if (Objects.isNull(brandTypeId)) {
            return new Message(MessageEnum.BRAND_TYPE_ID_IS_NULL);
        }
        PageInfo<Brand> pageInfo = brandService.listBrand(brandTypeId, pageNumber, pageSize);
        PageResult pageResult = new PageResult(pageInfo);
        return new Message(MessageEnum.GENERAL_REQUEST_SUCCESS, pageResult);
    }

}
